home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group98a.txt / 000130_icon-group-sender _Fri Mar 13 12:35:12 1998.msg < prev    next >
Internet Message Format  |  2000-09-20  |  4KB

  1. Return-Path: <icon-group-sender>
  2. Received: from kingfisher.CS.Arizona.EDU (kingfisher.CS.Arizona.EDU [192.12.69.239])
  3.     by baskerville.CS.Arizona.EDU (8.8.7/8.8.7) with SMTP id MAA14399
  4.     for <icon-group-addresses@baskerville.CS.Arizona.EDU>; Fri, 13 Mar 1998 12:35:12 -0700 (MST)
  5. Received: by kingfisher.CS.Arizona.EDU (5.65v4.0/1.1.8.2/08Nov94-0446PM)
  6.     id AA17700; Fri, 13 Mar 1998 12:35:12 -0700
  7. From: gep2@computek.net
  8. Date: Fri, 13 Mar 1998 11:30:44 -0600
  9. Message-Id: <199803131730.LAA18482@axp.cmpu.net>
  10. Mime-Version: 1.0
  11. Content-Type: text/plain
  12. Content-Transfer-Encoding: 7bit
  13. Subject: Letter Probabilities
  14. To: icon-group@optima.CS.Arizona.EDU
  15. X-Mailer: SPRY Mail Version: 04.00.06.17
  16. Errors-To: icon-group-errors@optima.CS.Arizona.EDU
  17. Status: RO
  18. Content-Length: 2730
  19.  
  20. > I have a table that associates inidividual letters (one-char strings)
  21. with real numbers (probabilities).  We can assume for the sake of
  22. argument that the sum of all probabilities in my table is unity.
  23.  
  24. > Given this table (which I already have Icon code to obtain) what is the
  25. most efficient method of generating random text?  What I am thinking of
  26. at the moment is:
  27.  
  28. > (1) get a sorted list of [key,value] pairs,
  29.        sorted by value (probability),
  30.        highest probability first
  31.  
  32. > (2) generate a random number from 0.0 to 1.0
  33.  
  34. > (3) use a while-loop to find the slot in the
  35.        sorted list where the number falls;
  36.        I would subtract each passing probability
  37.        until my placeholder value had vanished; e.g.
  38.  
  39. >       i := 0   # running index
  40.        x := ?0  # random number 0.0 - 1.0
  41.        while x > 0 do
  42.        {
  43.           i +:= 1
  44.           x -:= prob_list[i][2]
  45.        }
  46.        letter := prob_list[i][1]
  47.  
  48. You're trying to program it like you were programming in C, and that C-style 
  49. "clockwork mentality" is why you're having problems, IMHO.
  50.  
  51. What I think you ought to do is to simply take your table and build a string 
  52. (just once!) which contains a number of each letter commensurate with its 
  53. probability.  Then you can replace all this silly C-style nonsense with just:
  54.  
  55.      ?letterstring
  56.  
  57. ...and they will "automagically" come out (as many as you need) with the 
  58. probability you require.
  59.  
  60. In fact, you don't even need to start with your "probabilities" table at all... 
  61. you can just take your demonstration text, put it all in a string and remove any 
  62. characters you don't want there (quotation marks and other punctuation for 
  63. example) and you're all set.
  64.  
  65. > This all seems rather awkward to me, especially step (3).  
  66.  
  67. Yup, exactly, that's because you're using C-type programming mentality instead 
  68. of embracing an Icon-native approach to the problem.
  69.  
  70. > Isn't there some construct in Icon that could do this more elegantly?  
  71.  
  72. You BETCHA there is.  See above.  :-)
  73.  
  74. > P.S.  I am already very well acquainted with the sample program
  75. 'monkeys.icn' in the distribution.  This program uses multiple-character
  76. sequences, not individual letter probabilities.  More characters gives a
  77. better approximation to the source language, but I am interested
  78. specifically in single-character probabilities right at the moment.
  79.  
  80. You also ought to buy a copy of the fascinating book "Algorithms in SNOBOL4" by 
  81. Gimpel (and sold in facsimile reprint by Catspaw).  There are some exceedingly 
  82. nicely done programs there (and very, very useful functions) which deal with 
  83. random text generation too.
  84.  
  85. Gordon Peterson
  86. http://www.computek.net/public/gep2/
  87. Support the Anti-SPAM Amendment!  Join at http://www.cauce.org/
  88.  
  89.